Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1f  Compiler Error  The static main method can not access the non-static method m.  
2e  Prints: true,false,false  The right operand of the conditional "or" operator is not evaluated if the left hand operand is true. In this case the right hand operand of the conditional "or" operator is the result of the conditional "and" expression. Therefore, the conditional "and" expression is not evaluated.  
3b  Prints: false,false,true  The result of dividing two float values is a float. When the float is converted to a double a rounding error is introduced. For that reason, the value of variable a is different from the value of variables b and c.  
4d  Prints: false,true,true  The right operand of the conditional "or" operator is evaluated only if the left hand operand is false. In this case, the left operand of the conditional "or" operator is false so the right operand must also be evaluated. The right operand of the conditional "and" operator is only evaluated if the left hand operand is true. In this case, the left hand operand is true so the right hand expression is evaluated.  
5j  Compiler error  Both operands of the conditional "and" operator and conditional "or" operator must be of type boolean.  
6b  Prints: 2  Java evaluates expressions from left to right while respecting operator precedence. The multiplication operator has higher precedence than the logical and bitwise operators. The "and" operator has higher precedence than the "exclusive or" operator. The "or" operator has lower precedence. Although complete memorization of the operator precedence chart is not necessary, the level required for this question is required. (1 | 2 ^ 3 * 2 & 13 | 2), (1 | (2 ^ ((3 * 2) & 13)) | 2), (1 | (2 ^ ((6) & 13)) | 2), (1 | (2 ^ (4)) | 2), (1 | 6 | 2 = 7), (7%5=2)  
7b  Prints: 2,3  Primitive type variables are passed to methods by value. In other words, only the value of the variable is passed to the method so the method has no access to the variable itself. If the method modifies a primitive value that has been passed to the method as a parameter, then the modification does not impact the variable outside of the method. Instead, only the value of the local copy is modified. For that reason, the method m does not change the value of the variable y in the main method. However, method m does indeed have direct access to the member variable x and its value is modified by method m and the modification is seen within the main method.  
8a  Prints: 78 ABC*$  When char variables a and b are promoted to String types they are printed as *$. When not promoted to String types they are printed as the numeric sum of 0x2a and 0x24. Note: 0x2a is decimal 42 and 0x24 is decimal 36.  
9g  Compiler error at line 10.  The reference "base" is of type "Base[]", therefore an explicit cast to type "Base[]" is required.  
10j  Compiler error.  The result of the addition of a and b is an int. The attempt to assign the int result to a byte variable e generates a possible loss of precision error. The precedence of the cast operator is higher than the precedence of the addition operator. Therefore, the cast applies only to variable a and not to the result of the addition.  
11c  Prints: 1,2,3,4,5,3  Java evaluates expressions from left to right while respecting operator precedence. j = 1 + (((2 * 3) % 4) + 5)  
12g  Compiler error at line 5.  Although the referenced object is indeed an array of type int, an explicit cast is necessary to cast the obj reference to an int array.  
13b  Prints: BBABBA  The first method call, m(null,null), calls method m with two null literals that have type null. Both null literals are promoted to type B because B is more specific than type A. As a result, m(B x, B y) is invoked. The second method call, m(a=null,b=null), calls m with a first parameter of type A and a second parameter of type B. As a result, m(A x, B y) is invoked because the parameter types are matched exactly. The third method call invokes m(B x, A y) because the parameter types match exactly.  
14d  Prints: -1,127  The initial value of b is -1. The two's compliment representation of -1 is the binary value 11111111. If the left hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to an int. When a byte equal to minus 1 is promoted to an int the resulting int representation of minus one has all 32 bits set to one. When an int value of minus one is shifted right one bit the result is a sign bit of zero and the remaining 31 bits set to one. If that value were printed as an int it would be the maximum positive value of an int. However, if it is cast to a byte then it is reduced to eight bits that are all set to one. As stated earlier, that is the two's compliment representation of negative one. In the line marked with the number three the byte value of negative one is shifted to the right 25 bits. First, the byte is promoted to an int value of minus one. The result is all 32 bits of the int are set to one. When the int is shifted right 25 bits the result is 25 bits set to zero and the right most seven bits set to one. That is the binary representation of 127. When the int value of 127 is cast to a byte the result is also the value 127.  
15g  Compiles and runs without error.  All array types implement the Serializable interface and may be assigned to a reference of type Serializable.  
16f  Compiles and runs without error.  Although line 12 appears to be the source of a compiler error it is not. In line 12, the reference named base actually refers to an object of type Sub. Therefore, the reference to the object may be cast to type Sub.  
17a  Prints: float,float  An argument of type long can be widened to a float or a double. Since the float type is more specific than a double the float type is selected before double.  
18c  Prints: false,true,false  When Byte.MAX_VALUE is multiplied by two, the result is a byte with the most significant seven bits set to one and the least significant bit set to zero. The two's compliment representation of negative two is the same.  

Copyright © 2002, Dan Chisholm
All rights reserved.
1